package com.yjy.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.BaseUtils;
import com.system.comm.util.Snowflake;
import com.system.common.mapper.SysDeptMapper;
import com.system.common.mapper.SysRoleMapper;
import com.system.common.mapper.SysRoleUserMapper;
import com.system.common.mapper.SysUserMapper;
import com.system.common.pojo.SysDept;
import com.system.common.pojo.SysRole;
import com.system.common.pojo.SysRoleUser;
import com.system.common.pojo.SysRoleUserExample;
import com.system.common.pojo.SysUser;
import com.system.common.pojo.SysUserExample;
import com.system.common.pojo.SysUserExample.Criteria;

@Service
public class SysUserServiceImpl implements SysUserService{
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private SysRoleMapper roleMapper;
	
	@Autowired
	private SysRoleUserMapper ruMapper;
	
	@Autowired
	private SysDeptMapper deptMapper;

	@Override
	public List<SysUser> listAll(Map<?, ?> map) {
		List<SysUser> users=new ArrayList<>();
		List<SysUser> list=sysUserMapper.listAll(map);
		for (SysUser user : list) {
			if (user.getIsDel().equals("0")) {
				users.add(user);
			}
		}
		return users;
	}

	@Override
	public void save(SysUser user) {
		Snowflake sf=new Snowflake(0, 0);
		user.setUserId(sf.nextId()+"");
		user.setIsDel("0");
		user.setStyle("1");
		user.setPassword(BaseUtils.encrypt(user.getPassword(), BaseUtils.PASSWORD_KEY));
		sysUserMapper.insert(user);		
	}

	@Override
	public void update(SysUser user) {
		SysUser sysUser=sysUserMapper.selectByPrimaryKey(user.getUserId());
		user.setIsDel(sysUser.getIsDel());
		user.setCreateTime(sysUser.getCreateTime());
		user.setCreateUserId(sysUser.getCreateUserId());
		user.setStyle(sysUser.getStyle());
		user.setModifyTime(sysUser.getModifyTime());
		user.setModifyUserId(sysUser.getModifyUserId());
		user.setDeptId(sysUser.getDeptId());
		user.setPassword(BaseUtils.encrypt(user.getPassword(), BaseUtils.PASSWORD_KEY));
		sysUserMapper.updateByPrimaryKey(user);		
	}

	@Override
	public void delete(String uid) {
		SysUser user=sysUserMapper.selectByPrimaryKey(uid);
		user.setIsDel("1");
		sysUserMapper.updateByPrimaryKey(user);
	}

	@Override
	public SysUser getById(String uid) {
		SysUser user=sysUserMapper.selectByPrimaryKey(uid);
		if (user!=null) {
			return user;
		}
		return null;
	}

	@Override
	public SysUser getByAP(String account, String password) {
		SysUserExample example=new SysUserExample();
		Criteria cri=example.createCriteria();		
		cri.andAccountEqualTo(account);		
		cri.andPasswordEqualTo(password);
		List<SysUser> list=sysUserMapper.selectByExample(example);
		if (list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public SysRole getByUserId(String uid) {
		SysRoleUserExample example=new SysRoleUserExample();
		com.system.common.pojo.SysRoleUserExample.Criteria cri=example.createCriteria();
		cri.andUserIdEqualTo(uid);
		List<SysRoleUser> list=ruMapper.selectByExample(example);
		for (SysRoleUser sysRoleUser : list) {
			SysRole role=roleMapper.selectByPrimaryKey(sysRoleUser.getRoleId());
			return role;
		}
		return null; 
	}

	@Override
	public void move(String userId, String deptId) {
		SysUser user=sysUserMapper.selectByPrimaryKey(userId);
		SysDept dept=deptMapper.selectByPrimaryKey(deptId);
		user.setDept(dept);
		sysUserMapper.updateByPrimaryKey(user);		
	}

	@Override
	public List<SysUser> queryUserIds(String ids) {
		List<SysUser>  list=new ArrayList<>();
		if (ids.equals("0")) {
			SysUserExample example=new SysUserExample();
		    Criteria criteria=example.createCriteria();
		    criteria.andUserTypeEqualTo("1");
		    list= sysUserMapper.selectByExample(example);
		}else {
			list=sysUserMapper.queryUserIds(ids);
		}
		return list;
	}

	@Override
	public boolean checkName(String account) {
		SysUser user=sysUserMapper.checkName(account);
		if (user!=null) {
			return false;
		}
		return true;
	}

	@Override
	public boolean change(Map<String, Object> map) {
		if (map!=null) {
			sysUserMapper.change(map);
			return true;
		}		
		return false;
	}

}
